<?php
/**
 * 用户信息相关参数处理
 */
namespace Api\Helper;
use Api\Library\Helper;
use Api\Library\ErrorCode;
use Api\Library\Response;

class UserHelper extends Helper
{
    /**
     * 用户注册参数处理
     */
    public static function registerPostValid()
    {
        if(!isset(self::$ajax['username'], self::$ajax['password'], self::$ajax['mobile'], self::$ajax['idcard'], self::$ajax['blankcard'])) {
            Response::getInstance()->ajaxReturn(ErrorCode::ErrorParam, '参数不全');
        }

        self::checkUsername();
        self::checkPassword();
        self::checkMobile();
        self::checkIdCard();
        self::checkBlankCard();
    }

    /**
     * 用户登入参数验证
     */
    public static function loginPostValid()
    {
        if(!isset(self::$ajax['username'], self::$ajax['password'])) {
            Response::getInstance()->ajaxReturn(ErrorCode::ErrorParam, '参数不全');
        }

        self::checkUsername();
        self::checkPassword();
    }

    /**
     * 用户名验证
     */
    private static function checkUsername()
    {
        if(self::$ajax['username'] = self::stringValid(self::$ajax['username'], 1, 12)){
            self::$param['username'] = self::$ajax['username'];
        }else{
            Response::getInstance()->ajaxReturn(ErrorCode::InvalidParam, 'username长度错误');
        }
    }

    /**
     * 密码验证
     */
    private static function checkPassword()
    {
        if(self::$ajax['password'] = self::stringValid(self::$ajax['password'], 6, 12)) {
            self::$param['password'] = user_md5(self::$ajax['password'],'');
        }else {
            Response::getInstance()->ajaxReturn(ErrorCode::InvalidParam, 'password长度错误');
        }
    }

    /**
     * 手机号验证
     */
    private static function checkMobile()
    {
        if(self::$ajax['mobile'] = self::checkTel(self::$ajax['mobile'])) {
            self::$param['mobile'] = self::$ajax['mobile'];
        }else {
            Response::getInstance()->ajaxReturn(ErrorCode::InvalidParam, 'mobile格式错误');
        }
    }

    /**
     * 身份证号验证
     */
    private static function checkIdCard()
    {
        $idCard = self::doString(self::$ajax['idcard']);
        if(self::checkId($idCard)) {
            self::$param['idcard'] = $idCard;
        }else {
            Response::getInstance()->ajaxReturn(ErrorCode::InvalidParam, 'idcard格式错误');
        }
    }

    /**
     * 银行卡号验证
     */
    private static function checkBlankCard()
    {
        $blankCard = self::doString(self::$ajax['blankcard']);
        if(self::checkBlank($blankCard)) {
            self::$param['blankcard'] = $blankCard;
        }else {
            Response::getInstance()->ajaxReturn(ErrorCode::InvalidParam, 'blankcard格式错误');
        }
    }
}